import requests
from mysql import sqlExecute

"""
爬取全国疫情总体情况
"""

def get_url():
    # 抓取数据
    ## 先把数据都爬下来，查看数据结构，明确要整理保存的数据
    list = []
    # 国内疫情数据
    url_1 = 'https://api.inews.qq.com/newsqa/v1/query/inner/publish/modules/list?modules=chinaDayList,chinaDayAddList,nowConfirmStatis,provinceCompare'
    list.append(url_1)
    return list

# #第一步：爬取网页数据
def getData():
    url1 = get_url()[0]
    header = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.61 Safari/537.36"}
    result = requests.get(url=url1,headers = header)
    res = result.json()['data']
    # 每天数据
    res_list = res['chinaDayList']
    # 现在十分严重的
    list_nowSevere = []
    # 死亡比例
    list_deadRate = []
    # 治愈比例
    list_healRate = []
    # 日期
    list_date = []
    # 确诊
    list_confirm = []
    # 疑似病例
    list_suspect = []
    # 死亡
    list_dead = []
    # 治愈
    list_heal = []
    # 现确诊
    list_nowConfirm = []
    # 境外输入
    list_importedCase = []
    # 无症状感染者
    list_noInfect = []
    # 总数据
    list_sum1 = []
    for i in range(len(res_list)):
        list1 = []
        # 编号
        list1.append(i + 1)
        # 日期
        year = res_list[i]['y']
        date = res_list[i]['date']
        date1 = date.split('.')
        date2 = year + "-" + date1[0] + "-" + date1[1]
        list1.append(date2)
        list_date.append(date2)
        # 现在十分严重的
        nowSevere = res_list[i]['nowSevere']
        list1.append(nowSevere)
        list_nowSevere.append(nowSevere)
        # print(nowSevere)
        # 死亡比例
        deadRate = res_list[i]['deadRate']
        list1.append(deadRate)
        list_deadRate.append(deadRate)
        # print(deadRate)
        # 治愈比例
        healRate = res_list[i]['healRate']
        list1.append(healRate)
        list_healRate.append(healRate)
        # print(healRate)
        # 疑似病例
        suspect = res_list[i]['suspect']
        list1.append(suspect)
        list_suspect.append(suspect)
        # 确诊
        confirm = res_list[i]['confirm']
        list1.append(confirm)
        list_confirm.append(confirm)
        # 死亡
        dead = res_list[i]['dead']
        list1.append(dead)
        list_dead.append(dead)
        # 治愈
        heal = res_list[i]['heal']
        list1.append(heal)
        list_heal.append(heal)
        # 境外输入
        importedCase = res_list[i]['importedCase']
        list1.append(importedCase)
        list_importedCase.append(importedCase)
        # 无症状感染者
        noInfect = res_list[i]['noInfect']
        list1.append(noInfect)
        list_noInfect.append(noInfect)
        # 现确诊
        nowConfirm = res_list[i]['nowConfirm']
        list1.append(nowConfirm)
        list_nowConfirm.append(nowConfirm)

        list_sum1.append(list1)

    # 每日新增
    res_list1 = res['chinaDayAddList']
    # 总数据每日新增  用于预测新增
    list_sum2 = []

    for i in range(len(res_list1)):
        list2 = []
        list3 = []
        list4 = []
        # 日期
        year = res_list[i]['y']
        date = res_list1[i]['date']
        date1 = date.split('.')
        date2 = year +"-" + date1[0] +"-"+ date1[1]

        list2.append(date2)

        # 每日新增
        confirmadd = res_list1[i]['confirm']
        list2.append(confirmadd)

        # 每日死亡
        deadadd = res_list1[i]['dead']
        list2.append(deadadd)

        # 每日治愈
        healadd = res_list1[i]['heal']
        list2.append(healadd)

        list_sum2.append(list2)
    return list_sum1,list_sum2


# 第二步： 将数据保存到数据库中
def saveData():
    chinaDayAddList,chinaDayList = getData()

    # 每天疫情数据
    sqlDb = sqlExecute.MysqlDb()
    sql = "insert ignore into chinaDayAddList(date,confirmadd,deadadd,healadd)" \
          "values(%s,%s,%s,%s)"
    result1 = sqlDb.execute_sql(sql,chinaDayList)
    # 每天疫情总数据
    sqlDb = sqlExecute.MysqlDb()
    sql = "insert ignore into chinaDayList(id,date,nowSevere,deadRate,healRate,suspect,confirm,dead,heal,importedCase,noInfect,nowConfirm)" \
          "values(%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)"
    result2 = sqlDb.execute_sql(sql,chinaDayAddList)
    return result1,result2

def main():
    result1,result2 = saveData()
    print(result1)
    print(result2)

if __name__ == "__main__":
   main()